package com.core.family.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.core.family.admin.domain.Dict;
import com.core.family.admin.mapper.PanDataMapper;
import com.core.family.admin.service.DictService;
import com.core.family.admin.service.PanDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

import com.core.family.admin.domain.PanData;
import com.core.common.gen.BaseServiceImpl;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


/**
 * @author honglei
 * @version 1.0
 * @time: 2024-6-14 9:16:24
 * @Description 服务实现层 网盘资料表
 */
@Slf4j
@Service
public class PanDataServiceImpl extends BaseServiceImpl<PanDataMapper, PanData> implements PanDataService {


    @Override
    public int replaceBatch(List<PanData> panDatas) {
        if (CollectionUtils.isEmpty(panDatas)) {
            return 0;
        }
        return this.baseMapper.replaceBatch(panDatas);
    }

    @Resource
    DictService dictService;


    public List<PanData> formatBean(Map<String, List<Map<String, Object>>> mapList) {
        getDict(mapList);

        List<PanData> panDataList = new ArrayList<>();
        for (String sheetName : mapList.keySet()) {
            List<Map<String, Object>> sheetData = mapList.get(sheetName);
            for (Map<String, Object> rowdata : sheetData) {
                if (rowdata.isEmpty()) {
                    continue;
                }
                PanData panData = new PanData();
                panData.setDepartment(sheetName);
                panData.setCategory(sheetName);

                for (String key : rowdata.keySet()) {
                    //name
                    if (key.contains("名称")
                            || key.contains("文件名")
                            || key.contains("标题")
                    ) {
                        panData.setName(rowdata.get(key).toString());
                    }

                    //link
                    if (key.contains("链接")
                            || key.contains("外链")) {
                        String link = rowdata.get(key).toString();
                        panData.setLink(link);
                    }
                    //grp  分类
                    if (key.contains("分类")
                            || key.contains("类型")) {
                        String grp = rowdata.get(key).toString();
                        if (StringUtils.hasText(grp)) {
                            panData.setCategory(grp);
                        }
                    }
                }

                if (StringUtils.hasText(panData.getLink()) && panData.getLink().startsWith("http")) {
                    panDataList.add(panData);
                }
            }

        }

        return panDataList;

    }

    @Override
    public PanData simpleU(String str, String grp) {
        log.info("simpleU str:{}", str);
        PanData panData = new PanData();
        if (StringUtils.isEmpty(str)) {
            return panData;
        }
        if (str.contains("链接：")) {
            String[] strings = str.split("链接：");
            if (strings.length > 1) {
                panData.setName(strings[0]);
                panData.setLink(strings[1]);
            }
        }
        if (str.contains("链接:")) {
            String[] strings = str.split("链接:");
            if (strings.length > 1) {
                panData.setName(strings[0]);
                panData.setLink(strings[1]);
            }
        } else if (str.contains("http")) {
            String[] strings = str.split("http");
            if (strings.length > 1) {
                panData.setName(strings[0]);
                panData.setLink("http" + strings[1]);
            }
        }

        if (!StringUtils.isEmpty(panData.getLink())) {
            LambdaQueryWrapper<PanData> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(PanData::getLink, panData.getLink());

            saveOrUpdate(panData, wrapper);
        }
        log.info("simpleU str:{} \n pandata:{}", str, panData);
        return panData;
    }


    private void getDict(Map<String, List<Map<String, Object>>> mapList) {
        if (CollectionUtils.isEmpty(mapList)) {
            return;
        }
        for (String sheetName : mapList.keySet()) {
            if (StringUtils.isEmpty(sheetName)) {
                continue;
            }
            Dict dict = new Dict();
            dict.setDictName(sheetName);
            dict.setParentId("1");
            dict.setChildren(new ArrayList<>());
            if (CollectionUtils.isEmpty(mapList.get(sheetName))) {
                continue;
            }

            Set<String> dictName = new HashSet<>();
            List<Map<String, Object>> sheetData = mapList.get(sheetName);
            for (Map<String, Object> rowData : sheetData) {
                for (String key : rowData.keySet()) {
                    //grp  分类
                    if (key.contains("分类")
                            || key.contains("类型")) {
                        String grp = rowData.get(key).toString();
                        if (StringUtils.hasText(grp)) {
                            if (!dictName.contains(grp)) {
                                Dict dict1 = new Dict();
                                dict1.setDictName(grp);
                                dict1.setParentId(dict.getId());

                                dictName.add(grp);
                                dict.getChildren().add(dict1);
                            }
                        }
                    }
                }

            }

            dictService.saveDictFamily(dict);
        }

    }

}

